﻿namespace FileCategorizer.Engine.Data
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Linq;

    /// <summary>
    /// Implements the IDatabase interface for an in-memory database.
    /// This class should only be used for testing environments.
    /// </summary>
    public class MemoryDatabase : IDatabase
    {
        /// <summary>
        /// The tag database.
        /// </summary>
        private Dictionary<string, StringCollection> data = new Dictionary<string, StringCollection>();

        /// <summary>
        /// The directory database.
        /// </summary>
        private StringCollection directories = new StringCollection();

        #region IDatabase Members
        /// <summary>
        /// Gets a collection of all the paths currently in the directory
        /// database.
        /// </summary>
        /// <returns>A StringCollection of paths.</returns>
        public StringCollection Directories
        {
            get { return this.directories; }
        }

        /// <summary>
        /// Gets a value indicating whether there are tagged files in the 
        /// database.
        /// </summary>
        public bool IsEmpty
        {
            get { return this.data.Count == 0; }
        }

        /// <summary>
        /// Adds a path to the directory database.
        /// </summary>
        /// <param name="path">The path to add.</param>
        public void AddDirectory(string path)
        {
            this.directories.Add(path);
        }

        /// <summary>
        /// Removes a path from the directory database.
        /// </summary>
        /// <param name="path">The path to remove.</param>
        public void RemoveDirectory(string path)
        {
            this.directories.Remove(path);
        }

        /// <summary>
        /// Removes all paths from the directory database.
        /// </summary>
        public void ClearDirectories()
        {
            this.directories.Clear();
        }

        /// <summary>
        /// Adds a file to the tag database.
        /// </summary>
        /// <param name="path">The full path of the file to add.</param>
        public void AddFile(string path)
        {
            if (!this.data.ContainsKey(path))
            {
                this.data.Add(path, new StringCollection());
            }
        }

        /// <summary>
        /// Removes a file from the tag database, as well as all tags 
        /// associated with that file.
        /// </summary>
        /// <param name="path">The full path of the file to remove.</param>
        public void RemoveFile(string path)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Associates a specific tag to a file.
        /// </summary>
        /// <param name="path">The full path of the file.</param>
        /// <param name="tag">The tag to add to the file.</param>
        public void AddTag(string path, string tag)
        {
            // if the file isn't being tracked, add it first
            if (!this.data.ContainsKey(path))
            {
                this.AddFile(path);
            }

            // get the tag list
            StringCollection tags = this.data[path];
            if (!tags.Contains(tag))
            {
                tags.Add(tag);
            }
        }

        /// <summary>
        /// Removes a specific tag from a file.
        /// </summary>
        /// <param name="path">The full path of the file.</param>
        /// <param name="tag">The tag to remove from the file.</param>
        public void RemoveTag(string path, string tag)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets a value indicating whether this file has the particular
        /// tag.
        /// </summary>
        /// <param name="path">The full path to the file to check.</param>
        /// <param name="tag">The tag to find.</param>
        /// <returns><code>true</code> if the tag is mapped to the file.</returns>
        public bool HasTag(string path, string tag)
        {
            // if the file isn't in the database, then this if false.
            if (!this.data.ContainsKey(path))
            {
                return false;
            }

            // look up the tag
            StringCollection tags = this.data[path];
            return tags.Contains(tag);
        }

        /// <summary>
        /// Removes all files and tags from the database.
        /// </summary>
        public void ClearTags()
        {
            this.data.Clear();
        }

        /// <summary>
        /// Searches the tag database for the file name
        /// </summary>
        /// <param name="name">A string in the file path to locate</param>
        /// <returns>A collection of string matching the <code>name</code> substring.</returns>
        public StringCollection SearchByName(string name)
        {
            StringCollection files = new StringCollection();
            foreach (KeyValuePair<string, StringCollection> entry in this.data)
            {
                if (entry.Key.Contains(name) && !files.Contains(entry.Key))
                {
                    files.Add(entry.Key);
                }
            }

            return files;
        }

        /// <summary>
        /// Searches the tag database for the file with the specified tag.
        /// </summary>
        /// <param name="tag">The tag to find.</param>
        /// <returns>A collection of file paths mapped to the specified tag.</returns>
        public StringCollection SearchByTag(string tag)
        {
            StringCollection files = new StringCollection();
            foreach (KeyValuePair<string, StringCollection> entry in this.data)
            {
                if (entry.Value.Contains(tag) && !files.Contains(entry.Key))
                {
                    files.Add(entry.Key);
                }
            }

            return files;
        }
        #endregion
    }
}
